package commons;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import   org.apache.commons.collections.*;



public class UserRatingMap {
 private Map<Integer,Double>valori;
 private  double sumV;
 
 public Map<Integer,Double>getMappa(){
	 
	 return valori;
 }
	public UserRatingMap() {
		valori= new HashMap<Integer, Double>();
}
	public UserRatingMap(Map<Integer,Double> table){
		valori= new HashMap<Integer, Double>();
		valori.putAll(table);	
	}
	public int sum() {
		return valori.size();
	}

	public double getMean() {
		double sum=0;
		for (double app: valori.values()){
			sum +=app;
		}
		this.sumV=sum;
		return sum / (valori.size() + 1);
	}

 	public double getMeanDiversity() {
		int sum = 0;
		double mean_value = this.getMean();
		for (double d: valori.values()) {
			sum += Math.abs(d- mean_value);
		}
		
		return sum / valori.size();
	}
 public double getRating(int UserId) {
		if (valori == null || valori.size() == 0)
			return (Integer) null;
		else {
		double rating = valori.get(UserId);
		return rating;
		}
	}

	public void add(int UserId, double Rating) {
	valori.put(UserId,Rating);
	this.sumV +=Rating;
	
	
	}

	public HashMap<Integer,Double[]> intersect( Map<Integer,Double> table) {
		HashMap<Integer,Double[]> intersection = null;
		 CollectionUtils app;
		if ((valori.size() ==0) || (table.size()==0))	{ 
    		return null;}
    		else
    		{
    			intersection = new HashMap<Integer,Double[]>();
    			
   			// scorro sempre sulla mappa piu piccola
    		 
    			if (valori.size()<table.size()){
    				
    				for(int userId : valori.keySet()){
    					if(table.containsValue(userId)){
    						Double array[]= new Double[2];
    						array[0]=valori.get(userId);
    						array[1]=table.get(userId);
    						intersection.put(userId, array);
    					}
    				}
    			}
    			else{				
    				for(int userId : table.keySet()){
    		//System.out.println("Valore chiave "+ userId + "valori bool: " +valori.containsKey(userId) );
    		
    					if(valori.containsKey(userId)){
    						Double array[]= new Double[2];
    						array[0]=valori.get(userId);
    						array[1]=table.get(userId);
    						intersection.put(userId, array);
    					}
    				}
    			}
    				
  
    		}	
   
 	return intersection;
	 }
}